package com.zhiche.lisa.lspm.service.driver.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.enums.AttachtypeEnum;
import com.zhiche.lisa.core.enums.LicenseTypeEnum;
import com.zhiche.lisa.lspm.dao.mapper.driver.DriverLicenseMapper;
import com.zhiche.lisa.lspm.dao.model.driver.DriverLicense;
import com.zhiche.lisa.lspm.service.driver.IDriverLicenseService;
import com.zhiche.lisa.lspm.service.upload.UploadService;
import com.zhiche.lisa.lspm.service.utils.DevopUtil;
import com.zhiche.lisa.lspm.service.utils.MdmUtil;
import com.zhiche.lisa.lspm.vo.common.IdentityCardInfoVO;
import com.zhiche.lisa.lspm.vo.common.LicenseTypeVo;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 司机证照 service impl
 *
 * @author lbl
 * @since 2018-05-28
 */
@Service
public class DriverLicenseServiceImpl extends ServiceImpl<DriverLicenseMapper, DriverLicense> implements IDriverLicenseService {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private MdmUtil mdmUtil;
    @Autowired
    private DevopUtil devopUtil;

    @Override
    public void batchInsert(List<DriverLicense> list) {
        this.batchInsert(list);
    }

    @Override
    public void delete(Integer id) {
        this.deleteById(id);
    }

    @Override
    public void updateLicense(String key, Long driverLicenseId, String attachType, String authorization) {
        new Thread(() -> {
            String downloadUrl = uploadService.getStrUrl(key);
            //加入识别信息

            DriverLicense driverLicense = this.selectById(driverLicenseId);
            Long licenseTypeId = driverLicense.getLicenseTypeId();
            LicenseTypeVo licenseTypeVo = mdmUtil.getLicenseTypeById(licenseTypeId.toString());
            if (Objects.nonNull(licenseTypeVo)) {
                //身份证
                if (licenseTypeVo.getTypeCode().equals(LicenseTypeEnum.DRIVER_ID_CARD.getCode())) {
                    //正面
                    if (attachType.equals(AttachtypeEnum.POSITIVE.getCode())) {
                        IdentityCardInfoVO identityCardInfoVO = devopUtil.getIdentityCardInfo(downloadUrl, "1", authorization);
                        if (Objects.nonNull(identityCardInfoVO)) {
                            if (StringUtils.isNotEmpty(identityCardInfoVO.getNumber()))
                                driverLicense.setLicenseId(identityCardInfoVO.getNumber());
                            this.updateById(driverLicense);
                        }
                    }
                }

                //反面
                if (attachType.equals(AttachtypeEnum.REVERSE.getCode())) {
                    IdentityCardInfoVO identityCardInfoVO = devopUtil.getIdentityCardInfo(downloadUrl, "2", authorization);
                    if (Objects.nonNull(identityCardInfoVO)) {
                        if (StringUtils.isNotEmpty(identityCardInfoVO.getStart_date()))
                            driverLicense.setGmtReg(DateTime.parse(identityCardInfoVO.getStart_date(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate());
                        if (StringUtils.isNotEmpty(identityCardInfoVO.getEnd_date()))
                            driverLicense.setGmtOverdue(DateTime.parse(identityCardInfoVO.getStart_date(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate());
                        this.updateById(driverLicense);
                    }
                }
            }
        }).start();
    }

    @Override
    public List<DriverLicense> queryIDcard(Long driverId) {
        Wrapper<DriverLicense> driverLicenseWrapper = new EntityWrapper<>();
        driverLicenseWrapper.eq("driver_id", driverId).eq("license_type_id", 1);
        return baseMapper.selectList(driverLicenseWrapper);
    }

    @Override
    public DriverLicense selectOneNoTenant(Wrapper<DriverLicense> ew) {
        return baseMapper.selectOneNoTenant(ew);
    }

    @Override
    public List<DriverLicense> selectListNoTenant(EntityWrapper<DriverLicense> licenseEW) {
        return baseMapper.selectListNoTenant(licenseEW);
    }
}
